package com.qfmy.web.app.service.impl.novel;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.novel.CommentReplies;
import com.qfmy.model.entity.novel.CommentReplyLikes;
import com.qfmy.web.app.mapper.novel.CommentRepliesMapper;
import com.qfmy.web.app.mapper.novel.CommentReplyLikesMapper;
import com.qfmy.web.app.service.novel.CommentReplyLikesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.novel
 * @date 2025/10/31
 * @description 子评论的点赞
 */
@Slf4j
@Service
public class CommentReplyLikesServiceImpl implements CommentReplyLikesService {

    /**
     * 注入mapper
     */
    @Autowired
    private CommentReplyLikesMapper commentReplyLikesMapper;

    /**
     * 注入评论mapper
     */
    @Autowired
    private CommentRepliesMapper commentRepliesMapper;

    /**
     * 添加子评论的点赞
     * @param replyId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCommentReplyLikes(Integer replyId) {
        //获取登录用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_EXPIRED);
        }
        //获取用户id
        Long userId = loginUser.getUserId();

        //根据replyId查询评论信息
        CommentReplies commentReplies = commentRepliesMapper.selectCommentReplyById(replyId);
        if(commentReplies == null)
        {
            log.info("该评论已经被删除了");
            //说明不存在点赞信息
            throw new BaseException(ResultCodeEnum.COMMENT_NOT_EXIST);
        }

        //根据用户id和子评论id查询点赞信息
        CommentReplyLikes commentReplyLikes = commentReplyLikesMapper.getCommentReplyLikesByReplyId(replyId,userId);
        if(commentReplyLikes != null)
        {
            //更新点赞信息
            commentReplyLikesMapper.updateCommentReplyLikes(replyId,userId);
        }else{
            //添加点赞信息
            commentReplyLikesMapper.addCommentReplyLikes(replyId,userId);
        }
    }

    /**
     * 取消子评论的点赞
     * @param replyId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelCommentReplyLikes(Integer replyId) {
        //获取登录用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        Long userId = loginUser.getUserId();

        //根据replyId查询评论信息
        CommentReplies commentReplies = commentRepliesMapper.selectCommentReplyById(replyId);
        if(commentReplies == null)
        {
            log.info("该评论已经被删除了");
            //说明不存在点赞信息
            throw new BaseException(ResultCodeEnum.COMMENT_NOT_EXIST);
        }

        //根据用户id和子评论id查询点赞信息
        CommentReplyLikes commentReplyLikes = commentReplyLikesMapper.getCommentReplyLikesByReplyId(replyId,userId);
        if(commentReplyLikes == null)
        {
            log.info("该评论已经被删除了");
            //说明不存在点赞信息
            throw new BaseException(ResultCodeEnum.COMMENT_NOT_EXIST);
        }
       //更新点赞信息
        commentReplyLikesMapper.updateCommentReplyLikesDel(replyId,userId);
    }
}
